using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TheLogWatcher.Properties;

namespace TheLogWatcher
{
    public partial class LogEntryView : Form
    {
        public LogEntryView(LogEntry entry, int rowIndex, int availableRows)
        {
            InitializeComponent();
            this.entry = entry;
            this.rowIndex = rowIndex;
            this.availableRows = availableRows;
        }

        public LogEntryView(MainWindow mainWindow, int rowIndex, int availableRows)
        {
            InitializeComponent();
            this.entry = mainWindow.RetrieveNeededEntry(rowIndex);
            this.mainWindow = mainWindow;
            this.rowIndex = rowIndex;
            this.availableRows = availableRows;
        }

        private static Settings sets = Settings.Default;

        private LogEntry entry;
        private MainWindow mainWindow;
        private int rowIndex;
        private int availableRows;

        private void LogEntryView_Load(object sender, EventArgs e)
        {
            if (this.entry == null)
            {
                return;
            }
            this.textDate.Text = this.entry.Datetime.ToLongDateString() + "  (" + this.entry.Datetime.ToLongTimeString() + ")";

            this.richTextBox.Clear();
            this.richTextBox.AppendText(this.entry.Fields["Package"]);
            this.richTextBox.AppendText("\n" + this.entry.Message);

            if (this.entry.Type == MessageTypes.INFO)
            {
                this.pictureType.Image = Resources.ButtonInfoActive;
            }
            else if (this.entry.Type == MessageTypes.DEBUG)
            {
                this.pictureType.Image = Resources.ButtonDebugActive;
            }
            else if (this.entry.Type == MessageTypes.WARN)
            {
                this.pictureType.Image = Resources.ButtonWarnActive;
            }
            else if (this.entry.Type == MessageTypes.ERROR)
            {
                this.pictureType.Image = Resources.ButtonErrorActive;
            }
            else if (this.entry.Type == MessageTypes.FATAL)
            {
                this.pictureType.Image = Resources.ButtonFatalActive;
            }

            this.toolStripStatusLblNumber.Text = (rowIndex + 1).ToString();
            this.toolStripStatusLblMax.Text = availableRows.ToString();

            Font fnt = new Font(richTextBox.Font.FontFamily, richTextBox.Font.Size, FontStyle.Bold, GraphicsUnit.Point);

            // Format package declaration (blue)
            int pos = this.richTextBox.Find(this.entry.Package);
            if (pos > -1)
            {
                this.richTextBox.SelectionStart = pos;
                this.richTextBox.SelectionLength = this.entry.Package.Length;
                this.richTextBox.SelectionColor = Color.Blue;
                this.richTextBox.SelectionFont = fnt;
            }
            this.richTextBox.SelectionLength = 0;

            // SETTING WINDOW POSITION & SIZE
            if (sets.LogEntryViewPosX == 0 || sets.LogEntryViewPosY == 0)
            {
                sets.LogEntryViewPosX = this.Bounds.X;
                sets.LogEntryViewPosY = this.Bounds.Y;
            }
            this.Bounds = new Rectangle(sets.LogEntryViewPosX, sets.LogEntryViewPosY, sets.LogEntryViewWidth, sets.LogEntryViewHeight);

            // Set default save filename
            string filename = "Log entry from  " + this.entry.Datetime.Year + "-"
                + this.entry.Datetime.Month.ToString().PadLeft(2,'0') + "-"
                + this.entry.Datetime.Day.ToString().PadLeft(2, '0') + "  "
                + this.entry.Datetime.Hour.ToString().PadLeft(2, '0') + "-"
                + this.entry.Datetime.Minute.ToString().PadLeft(2, '0') + "-"
                + this.entry.Datetime.Second.ToString().PadLeft(2, '0');
            this.saveFileDialog.FileName = filename;

            // Set next and previous buttons accessibility
            if (this.rowIndex >= (this.availableRows - 1))
            {
                // End of entry list is reached
                this.nextToolStripButton.Enabled = false;
            }
            else
            {
                this.nextToolStripButton.Enabled = true;
            }
            if (this.rowIndex <= 0)
            {
                // End of entry list is reached
                this.previousToolStripButton.Enabled = false;
            }
            else
            {
                this.previousToolStripButton.Enabled = true;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LogEntryView_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Setting window size & position
            sets.LogEntryViewWidth = this.Width;
            sets.LogEntryViewHeight = this.Height;
            sets.LogEntryViewPosX = this.Bounds.X;
            sets.LogEntryViewPosY = this.Bounds.Y;
            sets.Save();
        }

        private void saveTxtToolStripButton_Click(object sender, EventArgs e)
        {
            this.saveFileDialog.DefaultExt = "txt";
            this.saveFileDialog.Filter = "Unicode Text Files|*.txt|All Files|*.*";
            this.saveFileDialog.FilterIndex = 0;
            DialogResult result = this.saveFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                string filename = this.saveFileDialog.FileName;
                this.richTextBox.SaveFile(filename, RichTextBoxStreamType.UnicodePlainText);
            }
        }

        private void saveRtfToolStripButton_Click(object sender, EventArgs e)
        {
            this.saveFileDialog.DefaultExt = "rtf";
            this.saveFileDialog.Filter = "Rich Text Files|*.rtf|All Files|*.*";
            this.saveFileDialog.FilterIndex = 0;
            DialogResult result = this.saveFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                string filename = this.saveFileDialog.FileName;
                this.richTextBox.SaveFile(filename, RichTextBoxStreamType.RichText);
            }
        }

        private void nextToolStripButton_Click(object sender, EventArgs e)
        {
            this.LogEntryView_FormClosing(null, null);
            this.entry = mainWindow.RetrieveNeededEntry(++this.rowIndex);
            this.LogEntryView_Load(null, null);
        }

        private void previousToolStripButton_Click(object sender, EventArgs e)
        {
            this.LogEntryView_FormClosing(null, null);
            this.entry = mainWindow.RetrieveNeededEntry(--this.rowIndex);
            this.LogEntryView_Load(null, null);
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = (ToolStripButton)sender;
            Point relPos = btn.Bounds.Location;
            Point absPos = new Button().PointToScreen(relPos);
            this.toolTipHelp.Active = true;
            string helpText = "The content of a single log file entry is shown here.\r\nIt is possible to save this content as Rich-Text file\r\nor as Unicode TXT file (save buttons to the left).\r\nFurthermore it is possible to switch to\r\na pevious or to the next entry by clicking the\r\ntoolstrip buttons containing arrows.";
            this.toolTipHelp.Show(helpText, this, absPos.X + btn.Bounds.Width, absPos.Y + btn.Bounds.Height, 8000);
        }
    }
}